package com.probiz.estore.common.model.system;

import java.util.HashSet;
import java.util.Set;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.base.AppRoleTbl;

public class AppRole extends AppRoleTbl implements GrantedAuthority{

	/** 角色-资源对象临时保存，主要用于保存页面参数*/
	private Set<AppResource> resourcesTemp = null;
	/** 角色-菜单对象临时保存，主要用于保存页面参数*/
	private Set<AppMenu> menusTemp = null;
	
	public Set<AppResource> getResourcesTemp() {
		return resourcesTemp;
	}

	public void addResourceTemp(AppResource appResource){
		if(resourcesTemp==null)
			this.resourcesTemp = new HashSet<AppResource>();
		this.resourcesTemp.add(appResource);
	}
	
	public Set<AppMenu> getMenusTemp() {
		return menusTemp;
	}

	public void addMenuTemp(AppMenu appMenu){
		if(menusTemp==null)
			this.menusTemp = new HashSet<AppMenu>();
		this.menusTemp.add(appMenu);
	}
	
	
	/* (non-Javadoc)
     * @see org.acegisecurity.GrantedAuthority#getAuthority()
     * 继承GrantedAuthority，返回授权资源
     */
    public String getAuthority() {
        return getRoleName();
    }

	/**
	 * Default Empty Constructor for class AppRole
	 */
	public AppRole () {
		super();
	}
	
	/**
	 * 定义实体的业务名取值； appRoleName
	 * 必须手工完成这个部分，否则编译不通过。
	 */
	public String getAppRoleName () {
		if (roleId == null)
	        return "";
	    else
			//返回一个指定有业务意义的属性值;
			//如：product的VO就用product.productName
	        return this.roleName;
	}
	
	/**
	 * Default Key Fields Constructor for class AppRole
	 */
	public AppRole (
		 Integer in_roleId
		) {
		super (
		  in_roleId
		);
	}

	public Integer getRolePathLevel(){
		return StringUtils.countMatches(this.getRolePath(), ".");
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
	
	public boolean getIsSuperAdminRole(){
		return Constants.DEFAULT_ADMIN_ROLE_NAME.equals(this.getRoleName());
	}
	
}
